<?php

namespace app\home\controller;

// 引入公共控制器
use app\common\controller\Home;

class Teacher extends Home
{

    protected $noNeedLogin = ['index', 'info'];

    public function _initialize()
    {
        // 调用父类构造函数
        parent::_initialize();

        $this->BusinessModel = model('Business.Business');
        $this->TeacherModel = model('Subject.Teacher.Teacher');
        $this->FollowModel = model('Subject.Teacher.Follow');
    }

    // 教师列表
    public function index()
    {
        if ($this->request->isPost()) {
            // 获取页码
            $page = $this->request->param('page', 1, 'intval');

            // 获取搜索关键字
            $keywords = $this->request->param('keywords', '', 'trim');

            $where = [];

            if ($keywords) {
                $where['name'] = ['like', "%{$keywords}%"];
            }

            // 每页显示数量
            $limit = 10;
            // 计算偏移量
            $offset = ($page - 1) * $limit;

            // 组装响应数据
            $data = [
                'list' => [],
                'count' => 0,
            ];

            // 查询课程列表
            $teacherList = $this->TeacherModel
                ->where($where)
                ->limit($offset, $limit)
                ->order('id desc')
                ->select();

            // 组装数据
            if ($teacherList) {
                $data['list'] = $teacherList;
                $data['count'] = $this->TeacherModel->count();


                // 响应数据
                $this->success('', '', $data);
            } else {
                // 响应数据
                $this->error('暂无数据', '', $data);
            }
        }

        return $this->view->fetch();
    }

    // 教师详情
    public function info()
    {
        $teacherid = $this->request->param('teacherid', 0, 'trim');

        $info = $this->TeacherModel
            ->find($teacherid);

        if (!$info) {
            $this->error('教师不存在');
            exit;
        }

        // 关注状态
        $info['follow_active'] = false;

        $Business = $this->IsLogin(false);
        if ($Business) {
            $follow = $this->FollowModel->where([
                'teacherid' => $teacherid,
                'busid' => $Business['id'],
            ])->find();

            $info['follow_active'] = $follow ? true : false;
        }

        $this->view->assign([
            'info' => $info,
        ]);

        return $this->view->fetch();
    }

    // 关注/取消关注
    public function follow()
    {
        $teacherid = $this->request->param('teacherid', 0, 'trim');

        $info = $this->TeacherModel
            ->find($teacherid);

        if (!$info) {
            $this->error('教师不存在');
            exit;
        }

        // 组装查询数据
        $data = [
            'teacherid' => $teacherid,
            'busid' => $this->Business['id'],
        ];

        // 获取关注状态
        $followinfo = $this->FollowModel->where($data)->find();

        if ($followinfo) {
            $result = $this->FollowModel->where($data)->delete();
            if ($result) {
                $this->success('取消关注成功');
            } else {
                $this->error('取消关注失败' . $this->FollowModel->getError());
            }
        } else {
            $result = $this->FollowModel->save($data);
            if ($result) {
                $this->success('关注成功');
                exit;
            } else {
                $this->error('关注失败' . $this->FollowModel->getError());
                exit;
            }
        }
    }
}
